package cn.jh.common.pay.utils;

import cn.jh.common.pay.jdpush.PayBarState;

import java.io.Serializable;
import java.util.Map;
import java.util.Objects;

import static com.google.common.base.Strings.isNullOrEmpty;


/**
 *
 * @author Yichuan
 * @version 1.0
 */
public class PayBarResponse implements Serializable {

    private static final long serialVersionUID = -878419550244357141L;

    public boolean isSuccess() {
        String bizCode = getField("bizCode");
        if (!isNullOrEmpty(bizCode) && !Objects.equals(bizCode, "1")) {
            return false;
        }
        return Objects.equals(getField("code"), "200") && state == PayBarState.SUCCESS;
    }

    private Map<String, String> resultMap;

    private PayBarState state;

    private String code;

    private String message;

    public PayBarResponse() {
        super();
    }

    public Map<String, String> getResultMap() {
        return resultMap;
    }

    public void setResultMap(Map<String, String> resultMap) {
        this.resultMap = resultMap;
    }

    public PayBarState getState() {
        return state;
    }

    public void setState(PayBarState state) {
        this.state = state;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getField(String field) {
        if (resultMap == null || resultMap.isEmpty()) return null;
        return resultMap.get(field);
    }

    public String getErrCode() {
        return (code == null && resultMap != null) ? resultMap.get("bizCode") : code;
    }
}
